﻿/***************************************************************
* File    : EngineeringReplay.cs
* Author  : Kyle Burgess
*
* Description:
*   Module used while building the system. Testing purposes only
*
* History :
***************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace RLGDashboard
{
    public class EngineeringReplay
    {
        private StreamReader fileHandle;
        private InQueue transmissionQueue;
        private Thread simulationThread;
        private bool simulatingActive;

        public EngineeringReplay(string fileName)
        {
            fileHandle = new StreamReader(System.IO.Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + @"\" + fileName);
            simulatingActive = false;
        }

        public void simulateBluetooth(ref InQueue transmissionQueue)
        {
            this.transmissionQueue = transmissionQueue;
            simulatingActive = true;

            simulationThread = new Thread(new ThreadStart(simulating));
            simulationThread.Start();
        }

        public bool isSimulating()
        {
            return simulatingActive;
        }

        private void simulating()
        {
            int timeBetweenMessages = 21;

            //Create a handle that we can use to make the thread wait.
            EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset);
            DateTime start2 = DateTime.Now;
            int counter = 0;

            while (simulatingActive && !fileHandle.EndOfStream)
            {

                DateTime start = DateTime.Now;
                BluetoothTransmission transmission = new BluetoothTransmission();
                readNewTransmission(transmission);

                transmissionQueue.writeToQueue(transmission);

                DateTime end = DateTime.Now;
                TimeSpan span = end - start;

                //Wait the difference between the normal next message and the time it took to process.
                if (timeBetweenMessages - span.Milliseconds > 0)
                {
                    handle.WaitOne(timeBetweenMessages - span.Milliseconds);
                }

                counter++;
                DateTime end2 = DateTime.Now;
                TimeSpan span2 = end2 - start2;
            }

            if (fileHandle.EndOfStream)
            {
                MessageBox.Show("Simulation file has ended");
            }
        }

        private void readNewTransmission(BluetoothTransmission transmission)
        {
            string value;
            do
            {
                value = fileHandle.ReadLine();
            } while (value != "NEW");

            transmission.timestamp = UInt32.Parse(fileHandle.ReadLine());
            transmission.messageID = byte.Parse(fileHandle.ReadLine());
            transmission.sensors[0] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[1] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[2] = UInt16.Parse(fileHandle.ReadLine());
            transmission.sensors[3] = UInt16.Parse(fileHandle.ReadLine());

            //read off un-needed "END"
            fileHandle.ReadLine();
        }

        public void killSimulation()
        {
            simulatingActive = false;

            EventWaitHandle handle = new EventWaitHandle(false, EventResetMode.AutoReset);

            while (simulationThread.IsAlive)
            {
                handle.WaitOne(10);
            }
        }
    }
}
